<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise-ajax的封装</title>
</head>
<body>


    <script>
        var getJSON = function(url){
            var promise = new Promise(function(resolve, reject){
                var client = new XMLHttpRequest();
                client.open("GET", url);
                client.onreadystatechange = handler;
                client.responseType = "json";
                client.setRequestHeader("Accept", "application/json");
                client.send();
                function handler() {
                    if (this.status === 200) {
                        resolve(this.response);
                    } else {
                        reject(new Error(this.statusText));
                    }
                };
            });
            return promise;
        };
    </script>

</body>
</html>